#!/bin/sh

cleanup () {
  unset -f nvm_has_colors
  unset NVM_COLORS
  case "${TEST_DIR-}" in
    /*) rm -rf "${TEST_DIR}" ;;
  esac
}
die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

TEST_DIR="$(mktemp -d)" || die 'unable to create a temp dir'
CALLS="${TEST_DIR}/calls"
OUT="${TEST_DIR}/out"
ERR="${TEST_DIR}/err"
ESC="$(printf '\033')"

unset NVM_COLORS NVM_HAS_COLORS INSTALLED_COLOR LTS_AND_SYSTEM_COLOR \
  CURRENT_COLOR NOT_INSTALLED_COLOR DEFAULT_COLOR

# answers yes only to the first call of a run, as a real terminal does: the
# calls nested in `$( )` would see a pipe on stdout
MOCK_COLORS=0
nvm_has_colors () {
  echo x >> "${CALLS}"
  [ "${MOCK_COLORS}" = 1 ] && [ "$(command wc -l < "${CALLS}" | command tr -d ' ')" = 1 ]
}

run () {
  : > "${CALLS}"
  nvm_set_colors "$@" > "${OUT}" 2> "${ERR}"
  EXIT_CODE=$?
  OUTPUT="$(command cat "${OUT}")"
  ERRORS="$(command cat "${ERR}")"
  NUM_CALLS="$(command wc -l < "${CALLS}" | command tr -d ' ')"
}

assert_rejected () {
  NVM_COLORS='bygre'
  export NVM_COLORS
  run "$@"
  [ "_${EXIT_CODE}" = '_17' ] || die "\`nvm_set_colors $*\` exit code: got >${EXIT_CODE}<, expected >17<"
  [ "_${OUTPUT}" = '_' ] || die "\`nvm_set_colors $*\` stdout: got >${OUTPUT}<, expected nothing"
  [ "_${ERRORS}" = '_' ] || die "\`nvm_set_colors $*\` stderr: got >${ERRORS}<, expected nothing"
  [ "_${NVM_COLORS}" = '_bygre' ] || die "\`nvm_set_colors $*\` changed NVM_COLORS: got >${NVM_COLORS}<, expected >bygre<"
  [ "_${NUM_CALLS}" = '_0' ] || die "\`nvm_set_colors $*\` checked color support for an invalid value"
}

assert_rejected
assert_rejected ''
assert_rejected 'rgby'
assert_rejected 'rgbyce'
assert_rejected 'rgbyx'
assert_rejected 'Rgbyc '
assert_rejected ' rgby'
assert_rejected 'rgbyE'
assert_rejected 'rgbyw'
assert_rejected 'rgb.c'
assert_rejected 'rgb*c'
assert_rejected '-rgby'
assert_rejected 'rg by'
assert_rejected '12345'

# only the first argument is considered
assert_rejected 'rgby' 'rgbyc'

MOCK_COLORS=0
unset NVM_COLORS
run 'rgbyc' 'x'
[ "_${EXIT_CODE}" = '_0' ] || die "\`nvm_set_colors rgbyc x\` exit code: got >${EXIT_CODE}<, expected >0<"
[ "_${NVM_COLORS-}" = '_rgbyc' ] || die "\`nvm_set_colors rgbyc x\` NVM_COLORS: got >${NVM_COLORS-}<, expected >rgbyc<"

# --- no color support: plain confirmation and a warning ---------------------

unset NVM_COLORS
MOCK_COLORS=0
run 'rGbYe'
[ "_${EXIT_CODE}" = '_0' ] || die "uncolored exit code: got >${EXIT_CODE}<, expected >0<"
EXPECTED="Setting colors to: r G b Y e
WARNING: Colors may not display because they are not supported in this shell."
[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "uncolored stdout: got >${OUTPUT}<, expected >${EXPECTED}<"
[ "_${ERRORS}" = '_' ] || die "uncolored stderr: got >${ERRORS}<, expected nothing"
[ "_${NVM_COLORS-}" = '_rGbYe' ] || die "uncolored NVM_COLORS: got >${NVM_COLORS-}<, expected >rGbYe<"
EXPORTED="$(command env | command sed -n 's/^NVM_COLORS=//p')"
[ "_${EXPORTED}" = '_rGbYe' ] || die "NVM_COLORS was not exported: got >${EXPORTED}<, expected >rGbYe<"

# --- color support: each letter is shown in its own color, no warning -------

unset NVM_COLORS
MOCK_COLORS=1
run 'BcMkW'
[ "_${EXIT_CODE}" = '_0' ] || die "colored exit code: got >${EXIT_CODE}<, expected >0<"
EXPECTED="Setting colors to: ${ESC}[1;34mB${ESC}[0m${ESC}[0;36mc${ESC}[0m${ESC}[1;35mM${ESC}[0m${ESC}[0;30mk${ESC}[0m${ESC}[1;37mW${ESC}[0m"
[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "colored stdout: got >${OUTPUT}<, expected >${EXPECTED}<"
[ "_${ERRORS}" = '_' ] || die "colored stderr: got >${ERRORS}<, expected nothing"
[ "_${NVM_COLORS-}" = '_BcMkW' ] || die "colored NVM_COLORS: got >${NVM_COLORS-}<, expected >BcMkW<"
EXPORTED="$(command env | command sed -n 's/^NVM_COLORS=//p')"
[ "_${EXPORTED}" = '_BcMkW' ] || die "NVM_COLORS was not exported: got >${EXPORTED}<, expected >BcMkW<"

# every accepted letter, in both branches
for COLORS in rRgGb BcCyY mMkKe WWWWW; do
  for MOCK_COLORS in 0 1; do
    unset NVM_COLORS
    run "${COLORS}"
    [ "_${EXIT_CODE}" = '_0' ] || die "\`nvm_set_colors ${COLORS}\` exit code: got >${EXIT_CODE}<, expected >0<"
    [ "_${NVM_COLORS-}" = "_${COLORS}" ] || die "\`nvm_set_colors ${COLORS}\` NVM_COLORS: got >${NVM_COLORS-}<, expected >${COLORS}<"
  done
done

# the working variables, color flag included, stay local
for VAR in INSTALLED_COLOR LTS_AND_SYSTEM_COLOR CURRENT_COLOR NOT_INSTALLED_COLOR DEFAULT_COLOR NVM_HAS_COLORS; do
  eval "LEAKED=\"\${${VAR}-unset}\""
  [ "_${LEAKED}" = '_unset' ] || die "nvm_set_colors leaked ${VAR}=>${LEAKED}<"
done

cleanup
